package com.poem.controller;

import com.poem.dto.*;
import com.poem.entity.Favorites;
import com.poem.result.PageResult;
import com.poem.result.Result;
import com.poem.service.FavoritesService;
import com.poem.vo.FavoritesPageWithAdmin;
import com.poem.vo.FavoritesWithPoemsOrTypes;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: FavoritesController
 * @Author: YLY
 * @Description:
 * @Date: 2025/10/15
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/favorites")
public class FavoritesController {

    @Resource
    private FavoritesService favoritesService;

    @GetMapping("/check")
    public Result check(@RequestParam String resourceType, Long resourceId) {
        log.info("检查收藏");
        // 如果收藏，返回true，否则返回false
        boolean check = favoritesService.checkFavorite(resourceType, resourceId);
        return Result.success(check);
    }

    /**
     * 批量检查收藏
     * 
     * @param favoritesCheckDTO
     * @return
     */
    @PostMapping("/batchCheck")
    public Result<Map<Long, Boolean>> batchCheckFavorite(@RequestBody FavoritesCheckDTO favoritesCheckDTO) {
        log.info("批量检查收藏：{}", favoritesCheckDTO);
        Map<Long, Boolean> statusMap = favoritesService.batchCheckFavorite(favoritesCheckDTO);
        return Result.success(statusMap);
    }

    /**
     * 添加收藏
     * 
     * @param favoritesAddDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody FavoritesAddDTO favoritesAddDTO) {
        log.info("添加收藏：{}", favoritesAddDTO);
        favoritesService.addFavorites(favoritesAddDTO);
        return Result.success();
    }

    /**
     * 删除收藏
     * 
     * @param favoritesDTO
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody FavoritesDTO favoritesDTO) {
        log.info("删除收藏：{}", favoritesDTO);
        favoritesService.deleteFavorites(favoritesDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * 
     * @param favoritesPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<FavoritesWithPoemsOrTypes>> pageQuery(FavoritesPageQueryDTO favoritesPageQueryDTO) {
        log.info("分页查询：{}", favoritesPageQueryDTO);
        PageResult<FavoritesWithPoemsOrTypes> pageResult = favoritesService.pageQuery(favoritesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量统计收藏数量
     * 
     * @param favoritesCountDTO
     * @return
     */
    @PostMapping("/batchCount")
    public Result<Map<Long, Integer>> batchCount(@RequestBody FavoritesCountDTO favoritesCountDTO) {
        log.info("批量统计收藏数量：{}", favoritesCountDTO);
        Map<Long, Integer> countMap = favoritesService.batchCount(favoritesCountDTO);
        return Result.success(countMap);
    }

    /**
     * 管理员分页查询收藏
     * 
     * @param favoritesPageQueryDTO
     * @return
     */
    @GetMapping("/admin/page")
    public Result<PageResult<FavoritesPageWithAdmin>> adminPageQuery(FavoritesPageQueryDTO favoritesPageQueryDTO) {
        log.info("管理员分页查询收藏：{}", favoritesPageQueryDTO);
        PageResult<FavoritesPageWithAdmin> pageResult = favoritesService.pageQueryWithAdmin(favoritesPageQueryDTO);
        return Result.success(pageResult);
    }

}
